package fr.obsmip.sedoo.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import fr.obsmip.sedoo.client.GlobalBundle;
import fr.obsmip.sedoo.client.ui.table.news.NewsList;
import fr.sedoo.commons.client.style.SedooStyleBundle;
import fr.sedoo.commons.client.util.ElementUtil;
import fr.sedoo.commons.client.widget.AnimatedDockLayoutPanel;
import fr.sedoo.commons.shared.domain.New;

public class WelcomeViewImpl extends AbstractSection implements WelcomeView {

	private static final int MAX = 100;
	private static final int MIN = 0;
	private static final int HALF = 50;
	private static final int DURATION = 750;

	private Presenter presenter;
	
	private boolean contentLoaded = false;
	
	@UiField
	DockLayoutPanel latestNewsWidget;
	
	@UiField
	DockLayoutPanel latestUpdateWidget;
	
	@UiField
	AnimatedDockLayoutPanel mainPanel;
	
	@UiField
	NewsList newsList;
	
	@UiField
	Image maximizeNews;
	
	@UiField
	Image minimizeNews;
	
	@UiField
	Image maximizeUpdates;
	
	@UiField
	Image minimizeUpdates;
	
	
	@UiField
	NewsList lastModifiedEntriesList;
	
	private static WelcomeViewImplUiBinder uiBinder = GWT
			.create(WelcomeViewImplUiBinder.class);

	interface WelcomeViewImplUiBinder extends UiBinder<Widget, WelcomeViewImpl> {
	}

	public WelcomeViewImpl() {
		super();
		initWidget(uiBinder.createAndBindUi(this));
		applyCommonStyle();
	}

	@Override
	public void setContent(List<New> lastNews, List<New> lastUpdates) {
		newsList.setRowData(lastNews);
		lastModifiedEntriesList.setRowData(lastUpdates);
		contentLoaded = true;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		
		this.presenter = presenter;
		newsList.setPresenter(presenter);
		lastModifiedEntriesList.setPresenter(presenter);
	}

	@Override
	public Presenter getPresenter() {
		
		return presenter;
	}

	@Override
	public boolean isContentLoaded() {
		return contentLoaded;
	}
	
	 @UiHandler("maximizeNews")
	  void onMaximizeNewsButtonClicked(ClickEvent event) {
		 ElementUtil.hide(maximizeNews);
		 ElementUtil.show(minimizeNews);
		 mainPanel.setWidgetSize(latestNewsWidget, MAX, DURATION);
		 mainPanel.setWidgetSize(latestUpdateWidget, MIN, DURATION);
	  }
	 
	@UiHandler("minimizeNews")
	  void onMinimizeNewsButtonClicked(ClickEvent event) {
		 ElementUtil.hide(minimizeNews);
		 ElementUtil.show(maximizeNews);
		 
		 mainPanel.setWidgetSize(latestNewsWidget, HALF, DURATION);
		 mainPanel.setWidgetSize(latestUpdateWidget, HALF, DURATION);
	  }

	 @UiHandler("maximizeUpdates")
	  void onMaximizeUpdatesButtonClicked(ClickEvent event) {
		 ElementUtil.hide(maximizeUpdates);
		 ElementUtil.show(minimizeUpdates);
		 mainPanel.setWidgetSize(latestUpdateWidget, MAX, DURATION);
		 mainPanel.setWidgetSize(latestNewsWidget, MIN, DURATION);
	  }
	 
	 @UiHandler("minimizeUpdates")
	  void onMinimizeUpdatesButtonClicked(ClickEvent event) {
		 ElementUtil.hide(minimizeUpdates);
		 ElementUtil.show(maximizeUpdates);
		 mainPanel.setWidgetSize(latestNewsWidget, HALF, DURATION);
		 mainPanel.setWidgetSize(latestUpdateWidget, HALF, DURATION);
	  }

	 
}
